<?php

namespace app\mobile\controller\home\game;

use app\BaseController;
use app\model\GameModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 游戏列表
     * @return bool|string
     */
    public function list(): bool|string
    {
        try {
            $GameModel = new GameModel();
            $data = $GameModel->field('id,name,url,num')->select();
            return success($data);
        } catch (\Exception $e){
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 更新游戏使用次数
     * @return bool|string
     */
    public function updateNum(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require|integer',
            ]);
            $GameModel = new GameModel();
            $info = $GameModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->inc('num')->save();
            return success();
        } catch (\Exception $e){
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

}